package org.jeecg.modules.web.shopApply.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.entity.ShopInfo;
import org.jeecg.modules.app.mapper.ShopMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.web.shopApply.entity.ShopApply;
import org.jeecg.modules.web.shopApply.mapper.ShopApplyMapper;
import org.jeecg.modules.web.shopApply.service.IShopApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 商家入住申请
 * @Author: jeecg-boot
 * @Date:   2022-06-19
 * @Version: V1.0
 */
@Service
public class ShopApplyServiceImpl extends ServiceImpl<ShopApplyMapper, ShopApply> implements IShopApplyService {

    @Autowired
    public  ShopApplyMapper shopApplyMapper;

    @Autowired
    public ShopMapper shopMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public int findShopApplyByUserNameOrTel(String username, String tel) {
        int result = 0;
        QueryWrapper<ShopApply> queryWrapper = new QueryWrapper<>();
        ShopApply shopApply = shopApplyMapper.selectOne(queryWrapper.eq("username",username));
        if(shopApply != null){
            result += 1; //用户名重复
        }
        queryWrapper = new QueryWrapper<>();
        shopApply = shopApplyMapper.selectOne(queryWrapper.eq("tel",tel));
        if(shopApply != null){
            result += 2; //手机号重复
        }

        // 0 成功
        // 1 用户名已存在
        // 2 手机号已存在
        // 3  用户名 手机号已存在
        return result;
    }

    /**
     * 办理入驻申请
     * 1.把新用户添加进user表
     * 2.添加新的商家信息
     * 3.更新申请状态
     * @param shopApply
     */
    @Override
    public void agreeShopApply(ShopApply shopApply) {
        //1.把新用户添加进user表
       String userId =  this.addToUser(shopApply);

       //2.添加新的商家信息
       this.addToShopInfo(shopApply,userId);

       //3.更新申请状态
       shopApply.setStatus("已办理");

       QueryWrapper<ShopApply> queryWrapper = new QueryWrapper<>();
       shopApplyMapper.update(shopApply,queryWrapper.eq("id",shopApply.getId()));
    }

    /**
     * 往用户表加数据
     * @param shopApply
     * @return
     */
    public String addToUser(ShopApply shopApply){
        String username = shopApply.getUsername();
        String password = shopApply.getPassword();
        String email = null;
        String phone = shopApply.getTel();
        SysUser user = new SysUser();
        user.setCreateTime(new Date());// 设置创建时间
        String salt = oConvertUtils.randomGen(8);
        String passwordEncode = PasswordUtil.encrypt(username, password, salt);
        user.setSalt(salt);
        user.setUsername(username);
        user.setRealname(username);
        user.setPassword(passwordEncode);
        user.setEmail(email);
        user.setPhone(phone);
        user.setStatus(CommonConstant.USER_UNFREEZE);
        user.setDelFlag(CommonConstant.DEL_FLAG_0);
        user.setActivitiSync(CommonConstant.ACT_SYNC_0);
        String userId = sysUserService.myAddUserWithRole(user,"1538078731900309506");//默认临时角色 test
        return userId;
    }

    /**
     * 往商家表加数据
     * @param shopApply
     * @return
     */
    public String addToShopInfo(ShopApply shopApply,String userId){

        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setCreateTime(new Date()); // 创建时间
        shopInfo.setCreateBy(shopApply.getUsername()); // 创建人
        shopInfo.setName(shopApply.getShopName());  //店铺名称
        shopInfo.setShopUserId(userId); // 店主id
        shopInfo.setShopLicence(shopApply.getBusinessLicense()); // 营业执照
        shopInfo.setAddress(shopApply.getAddress());//商家地址 /省 市 县
        shopInfo.setAddressDetail(shopApply.getAddressdetail());//商家具体地址 //门牌号
        shopInfo.setAddressCodeArray(shopApply.getAddressCodeArray());// 地址code
        shopMapper.insert(shopInfo);
        return shopInfo.getId();
    }
}
